﻿/**
* QSmart Andon
*
* 2012-10-23
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    public class LevelDetailService : DbHelperBase
    {
        public int GetLevelDetailCount(LevelDetailQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append(" SELECT COUNT(*) AS [count] FROM UT_LevelDetail AS A  ");
            strSql.Append(" LEFT JOIN UT_User AS  B ON A.UserID=B.UserID ");
            strSql.Append(" LEFT JOIN UT_Department AS C ON B.DptID=C.DptID ");
            strSql.Append(" WHERE A.LvID=@LvID ");
            var count = 0;

            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LvID", qcmodel.LvID);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public List<LevelDetailEntity> GetLevelDetailList(LevelDetailQcEntity qcmodel)
        {
            var list = new List<LevelDetailEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by A.InputTime) AS idx,A.RowID,A.UserID,B.LoginCode,B.FullName,B.MobileNo,C.DptName FROM UT_LevelDetail AS A  ");
            strSql.Append(" LEFT JOIN UT_User AS  B ON A.UserID=B.UserID ");
            strSql.Append(" LEFT JOIN UT_Department AS C ON B.DptID=C.DptID ");
            strSql.Append(" WHERE LvID=@LvID");
            strSql.Append(" ) AS T ");

            strSql.Append(" Where T.idx between " + start + " and " + end);

            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {

                    Db.AddInParameter(cmd, "@LvID", qcmodel.LvID);

                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new LevelDetailEntity();
                            if (reader["RowID"] != DBNull.Value)
                                model.RowID = new Guid(reader["RowID"].ToString());

                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            model.MobileNo = reader["MobileNo"] == obj ? null : Convert.ToString(reader["MobileNo"]);
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;

        }

        public List<LevelDetailEntity> GetLevelDetailListByLvInfo(LevelDetailQcEntity qcmodel)
        {
            var list = new List<LevelDetailEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT B.RowID,B.UserID,C.LoginCode,C.FullName,C.QCode,D.DptName,C.MobileNo FROM UT_Level AS A ");
            strSql.Append(" INNER JOIN dbo.UT_LevelDetail AS B ON A.LvID=B.LvID ");
            strSql.Append(" INNER JOIN dbo.UT_User AS C ON B.UserID=C.UserID ");
            strSql.Append(" INNER JOIN dbo.UT_Department AS D ON D.DptID=C.DptID ");
            strSql.Append(" WHERE A.LvID=@LvID AND A.LevelIdx=@LevelIdx ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LvID", qcmodel.LvID);
                    Db.AddInParameter(cmd, "@LevelIdx", qcmodel.LevelIdx);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new LevelDetailEntity();
                            if (reader["RowID"] != DBNull.Value)
                                model.RowID = new Guid(reader["RowID"].ToString());
                            if (reader["UserID"] != DBNull.Value)
                                model.UserID = new Guid(reader["UserID"].ToString());
                            model.LoginCode = reader["LoginCode"] == obj ? null : Convert.ToString(reader["LoginCode"]);
                            model.FullName = reader["FullName"] == obj ? null : Convert.ToString(reader["FullName"]);
                            model.QCode = reader["QCode"] == obj ? null : Convert.ToString(reader["QCode"]);
                            model.DptName = reader["DptName"] == obj ? null : Convert.ToString(reader["DptName"]);
                            model.MobileNo = reader["MobileNo"] == obj ? null : Convert.ToString(reader["MobileNo"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public void AddLevelDetail(List<LevelDetailEntity> list)
        {
            var conn = Db.GetConnection() as SqlConnection;
            if (conn == null)
            {
                throw new InvalidOperationException("No Valid Database Connection!");
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            using (var bcp = new SqlBulkCopy(conn))
            {
                var mapRowId =
                    new SqlBulkCopyColumnMapping("RowID", "RowID");
                bcp.ColumnMappings.Add(mapRowId);

                var mapLvId =
                    new SqlBulkCopyColumnMapping("LvID", "LvID");
                bcp.ColumnMappings.Add(mapLvId);

                var mapUserId =
                    new SqlBulkCopyColumnMapping("UserID", "UserID");
                bcp.ColumnMappings.Add(mapUserId);

                bcp.DestinationTableName = "UT_LevelDetail";

                try
                {
                    bcp.WriteToServer(Collection.ToDataTable(list));
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
        }

        public void Remove(List<LevelDetailEntity> list)
        {
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_LevelDetail Where RowID=@RowID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@RowID", DbType.Guid);
                    foreach (var i in list)
                    {
                        cmd.Parameters["@RowID"].Value = i.RowID;
                        Db.ExecuteNonQuery(cmd);
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
        }

        public bool RemoveLevelDetail(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" Delete From UT_LevelDetail Where LvID=@LvID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@LvID", id);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
